﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FinanSoft.Entities;
using FinanSoft.BusinessLayer;
using FinanSoft.Utilities;

namespace FinanSoft.PresentacionInventario
{
    public partial class frmConsultaBodegas : Form
    {
        List<int> _storeIds;

        public frmConsultaBodegas()
        {
            InitializeComponent();
            fillGridBodegas();
        }

        private void btnCrearBodega_Click(object sender, EventArgs e)
        {
            frmCrearBodega frmCrearBodegaV = new frmCrearBodega(Constants.INVALID_ID);
            frmCrearBodegaV.ShowDialog(this);     // Mostramos el formulario de forma modal.
            fillGridBodegas();
        }

        private void btnSalir_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void addRow(StoreRoom store)
        {
            _storeIds.Add(store.Id);
            gridBodegas.Rows.Add(new object[] {store.Code, store.Name});
        }

        private void fillGridBodegas()
        {
            gridBodegas.Rows.Clear();
            List<StoreRoom> stores = StoreRoomManager.GetInstance().GetStoreRooms();
            if (!StoreRoomManager.GetInstance().IsError)
            {
                _storeIds = new List<int>();
                foreach (StoreRoom store in stores)
                    addRow(store);
            }
            else
            {
                MessageBox.Show(StoreRoomManager.GetInstance().ErrorMessage, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                Close();
            }
        }

        private void btnEditarBodega_Click(object sender, EventArgs e)
        {
            // Cargamos y mostramos el formulario Crear Cuenta
            if (gridBodegas.SelectedRows.Count == 0)
                MessageBox.Show(MessageConstants.NO_SELECTED_STORAGE, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            else
            {
                int editedId = _storeIds[gridBodegas.SelectedRows[0].Index];
                frmCrearBodega frmCrearBodegaV = new frmCrearBodega(editedId);
                frmCrearBodegaV.ShowDialog(this);     // Mostramos el formulario de forma modal.
                fillGridBodegas();
            }
        }

        private void btnEliminarBodega_Click(object sender, EventArgs e)
        {
            // Cargamos y mostramos el formulario Crear Cuenta
            if (gridBodegas.SelectedRows.Count == 0)
                MessageBox.Show(MessageConstants.NO_SELECTED_STORAGE, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            else
            {
                int deletedId = _storeIds[gridBodegas.SelectedRows[0].Index];
                if (!StoreRoomManager.GetInstance().DeleteStorage(deletedId))
                    MessageBox.Show(StoreRoomManager.GetInstance().ErrorMessage, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                {
                    MessageBox.Show(MessageConstants.STORE_DELETED, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    fillGridBodegas();
                }
            }
        }
    }
}
